//监听消息事件
//chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
//    var content = request.cmd;
//    if (content.length > 0) {
//        sendResponse("send success");
//        alert(content);
//    }
//});


console.log("jquery version:");
console.log(jQuery.fn.jquery);
console.log($.fn.jquery);
console.log("the time: " + new Date() + ",init value...");

var chatWindowsOpenFlag = false;  //聊天窗口是否打开
var isRunFlag = false;  //是否开启了监听,只监听一次click事件
var scanMax = 60000;  //查询次数最大值
var scanCount = 0;  //查询次数
var sleepTime = 30000;  //休眠时间,毫秒
var sendMessageContent = "Dear, thank you for your interest in our products. All the items are IN STOCK. " +
    "We do not provide free shipping, but there is some postage DISCOUNT which is set by the platform, " +
    "you can check it when you make your order. " +
    "We recommend that you choose LWE as shipping by China Post is not steady sometimes. " +
    "If you like our store, please FOLLOWING our shop and the items you like, " +
    "then you will get the everyday information about the new items. " +
    "Please note that we work from 9 am 6 pm（local time).";

var sendChinaMessageContent="您好，感謝您關注我家的產品。" +
    "我們一般先從上海發貨至深圳，貨到深圳以後才會顯示物流信息哦，" +
    "貨期一般是5-10天.收貨之後請認真檢查貨物是否完整或損壞，有任何問題請隨時聯繫我們，" +
    "如無誤再請確認收貨。如果您喜歡我們的產品，請關注我們的店鋪，這樣您就能查看到每天的產品更新信息。" +
    "有任何問題請隨時聯繫我們客服，工作時間9am-6pm. ";

console.log("loading....");


$(document).ready(function () {
    console.log("start run auto script");
    auto2();  //开始监听
});

$(document).ready(function () {
    window.setTimeout(function () {
        clickChatButton();
    }, sleepTime);
});


/**
 * 自动执行脚本
 */
function auto() {
    scanCount++;
    if (scanCount >= scanMax) {
        //大于扫描次数的时候刷新当前页面
        location.reload();
    }
    console.log(new Date());
    var list = findChatWindowAllUser();

    if (!list || list.length <= 0) {
        console.log("chat window is empty");
        return;
    }

    var b = findHaveNewMessageChatDiv(list);

    if (b && b.length > 0) {
        console.log("new message:" + b.length);
        focusAndClickHaveNewMessageChatWindow(b[0]);
    } else {
        console.log("new message is empty");
    }
}


/**
 * 找到所有聊天窗口所有的这些用户的div
 * @returns {XMLList}
 */
function findChatWindowAllUser() {
    var a = $("div#shopee-chat-embedded .show-offer");
    if (a) {
        return a.children().children().children().children();
    }
}

/**
 * 显示传过来的用户聊天窗口列表的名字
 * @param list
 */
function showChatWindowName(list) {
    for (var i = 0; i < list.length; i++) {
        var personChatWindow = list[i];
        var nameDiv = $(personChatWindow).find(".shopee-chat__name");
        console.log(nameDiv.html());
    }
}


/**
 * 找到传过来的聊天窗口的有新消息的窗口
 * @param list
 * @returns {Array}
 */
function findHaveNewMessageChatDiv(list) {
    if (list && list.length > 0) {
        var result = [];
        for (var i = 0; i < list.length; i++) {
            var personChatWindow = list[i];
            var badgeDiv = $(personChatWindow).find(".badge");
            if (badgeDiv && badgeDiv.hasClass("active")) {
                result.push(list[i]);
            }
        }
        return result;
    }
}

/**
 * 点击聊天人的按钮
 * @param w
 */
function focusAndClickHaveNewMessageChatWindow(w) {
    if (w) {
        var aTag = w.getElementsByTagName("a");
        if (aTag && aTag.length > 0) {
            //如果该a标签存在,则点击他.
            aTag[0].click();
            //这样聊天窗口就打开了
            sendMessage(sendChinaMessageContent);
        }
    }
}

/**
 * 往聊天窗口写入要发送的内容,且自动提交
 * @param content
 */
function sendMessage(content) {
    var t = $(".ember-text-area");
    t.val(content);
    t.focus();
    $(".shopee-chat__primary").focus();
    document.getElementsByClassName("shopee-chat__primary")[0].click();
    console.log("send content:" + content);
}


/**
 * 监听点击聊天按钮的事件,如果点击了则开始监听
 */
function auto2() {
    if (isRunFlag == false) {
        isRunFlag = true;
        $("#shopee-chat-embedded").on("click", function () {
            window.setInterval(function () {
                auto();
            }, sleepTime);
        });
    }
}

/**
 * 点击聊天按钮来显示聊天窗口
 */
function clickChatButton() {
    console.log("auto click chat button start");
    var a = $("#shopee-chat-embedded").children();
    var b = $(a).children();
    var c = b[0];
    var d = $(c).children();
    d[0].click();
    console.log("auto click chat button success");
    chatWindowsOpenFlag = true;
}


function hxy() {
    console.log("123")
}



















